﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyPSDALayer;
using MyPS.Models;
using MyPS.Areas.Manage.Models;
using System.Data;
using System.Data.SqlClient;


namespace MyPS.Areas.Manage.Controllers
{
    [Authorize]
    public class ManageRolesController : Controller
    {
        private MyPSEntities db = new MyPSEntities();

        //
        // GET: /Manage/ManageRoles/

        public ActionResult Index()
        {
            return View(db.Roles.ToList());
        }

   

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Manage/ManageRoles/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Role role)
        {
            if (ModelState.IsValid)
            {
                role.CreateBy = HttpContext.User.Identity.Name;
                role.CreateDate = DateTime.Now;
                role.ModifyBy = HttpContext.User.Identity.Name;
                role.ModifyDate = DateTime.Now;
                db.Roles.Add(role);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(role);
        }

        //
        // GET: /Manage/ManageRoles/Edit/5

        public ActionResult Edit(string roleid = "")
        {
            Role role = db.Roles.Find(roleid);
            if (role == null)
            {
                return HttpNotFound();
            }
            return View(role);
        }

        //
        // POST: /Manage/ManageRoles/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Role role)
        {
            if (ModelState.IsValid)
            {
                role.ModifyBy = HttpContext.User.Identity.Name;
                role.ModifyDate = DateTime.Now;
                db.Entry(role).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(role);
        }

        //
        // GET: /Manage/ManageRoles/Delete/5

        public ActionResult Delete(string roleid = "")
        {
            Role role = db.Roles.Find(roleid);
            if (role == null)
            {
                return HttpNotFound();
            }
            return View(role);
        }

        //
        // POST: /Manage/ManageRoles/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string roleid = "")
        {
            Role role = db.Roles.Find(roleid);
            db.Roles.Remove(role);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult RoleAuthor(string roleid = "")
        {
            var mode = db.Database.SqlQuery<RoleAuthorModel>(@"select a.ResourceID,a.ResourceName,a.Mode,b.IS_Del,b.IS_Edit,b.IS_View,b.IS_Add
from Resources a with(nolock) 
left join ResoucesAcl b with(nolock) on a.ResourceID=b.ResourceID and b.RoleID=@RoleID", new SqlParameter("@RoleID", SqlDbType.NVarChar, 50) { Value = roleid }).ToList();
            ViewBag.RoleID = roleid;
            return View(mode);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}